<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="dataForm.projectName" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="项目编码" prop="projectCode">
        <el-input v-model="dataForm.projectCode" placeholder="编码"></el-input>
      </el-form-item>
      <el-form-item label="项目地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="项目地址"></el-input>
      </el-form-item>
      <el-form-item label="项目经度" prop="lon">
        <el-input
          type="number"
          v-model="dataForm.lon"
          placeholder="项目经度"
        ></el-input>
      </el-form-item>
      <el-form-item label="项目纬度" prop="lat">
        <el-input
          type="number"
          v-model="dataForm.lat"
          placeholder="项目纬度"
        ></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="contacts">
        <el-input
          v-model="dataForm.contacts"
          placeholder="项目负责人"
        ></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="telephone">
        <el-input
          type="number"
          v-model="dataForm.telephone"
          placeholder="联系电话"
        ></el-input>
      </el-form-item>
      <el-form-item label="建设单位" prop="constructUnit">
        <el-select
          clearable
          v-model="dataForm.constructUnit"
          placeholder="建设单位"
          style="width: 100%;"
        >
          <el-option
            v-for="item in constructList"
            :key="item.id"
            :label="item.companyName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="施工单位" prop="constructionUnit">
        <el-select
        style="width: 100%;"
          clearable
          v-model="dataForm.constructionUnit"
          placeholder="施工单位"
        >
          <el-option
            v-for="item in constructionList"
            :key="item.id"
            :label="item.companyName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      options: [],
      optionsStatus: [],
      visible: false,
      dataForm: {
        id: 0,
        projectName: "",
        projectCode: "",
        address: "",
        lon: "",
        lat: "",
        contacts: "",
        telephone: "",
        constructUnit: "",
        constructionUnit: ""
      },
      dataRule: {
        projectName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        projectCode: [
          { required: true, message: "编码不能为空", trigger: "blur" }
        ]
        // address: [
        //   { required: true, message: "项目地址不能为空", trigger: "blur" }
        // ],
        // longitude: [
        //   { required: true, message: "项目经度不能为空", trigger: "blur" }
        // ],
        // latitude: [
        //   { required: true, message: "项目纬度不能为空", trigger: "blur" }
        // ],
        // leader: [
        //   { required: true, message: "项目负责人不能为空", trigger: "blur" }
        // ],
        // contactPhone: [
        //   { required: true, message: "联系电话不能为空", trigger: "blur" }
        // ],
        // constructionUnit: [
        //   { required: true, message: "施工单位不能为空", trigger: "blur" }
        // ],
      },
      constructList: [], //建设单位
      constructionList: [] // 施工单位
    };
  },
  mounted() {
    this.getCompany();
  },
  methods: {
    // 获取车辆所属公司
    getCompany() {
      this.$http({
        url: this.$http.adornUrl(`/sys/bcompany/all?typeId=GC0001001`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.constructList = data.list;
        }
      });
      this.$http({
        url: this.$http.adornUrl(`/sys/bcompany/all?typeId=GC0001005`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.constructionList = data.list;
        }
      });
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/project/management/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.projectName = data.info.projectName;
              this.dataForm.projectCode = data.info.projectCode;
              this.dataForm.address = data.info.address;
              this.dataForm.lon = data.info.lon;
              this.dataForm.lat = data.info.lat;
              this.dataForm.contacts = data.info.contacts;
              this.dataForm.telephone = data.info.telephone;
              this.dataForm.constructUnit = data.info.constructUnit;
              this.dataForm.constructionUnit = data.info.constructionUnit;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/project/management/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              projectName: this.dataForm.projectName,
              projectCode: this.dataForm.projectCode,
              address: this.dataForm.address,
              lon: this.dataForm.lon,
              lat: this.dataForm.lat,
              contacts: this.dataForm.contacts,
              telephone: this.dataForm.telephone,
              constructUnit: this.dataForm.constructUnit,
              constructionUnit: this.dataForm.constructionUnit
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
